. "$PSScriptRoot/common.ps1"
# 清空打包目录下的所有 nupkg 文件
Get-ChildItem -Path $packFolder -Filter *.nupkg -Recurse | Remove-Item -Force

$i = 0
$projectsCount = $projects.length
foreach ($project in $projects) {
    $i += 1
    $projectFolder = Join-Path $rootFolder $project
    $projectName = ($project -split '/')[-1]
    Write-Info "[$i / $projectsCount] - Packing project: $projectName"
    Set-Location $projectFolder
    
    dotnet pack -c Release -- /maxcpucount
    if (-Not $?) {
        Write-Error "Packaging failed for the project: $projectName"
        exit $LASTEXITCODE
    }
    
    # 使用包名规则查找nupkg文件
    $packageId = Get-PackageId -projectName $projectName
    $projectPackPath = Join-Path $projectFolder ("/bin/Release/" + $packageId + ".*.nupkg")
    $nupkgFiles = Get-ChildItem -Path $projectPackPath
    if ($nupkgFiles) {
        Move-Item -Force $nupkgFiles.FullName $packFolder
        Write-Info "已移动包文件: $($nupkgFiles.Name)"
    } else {
        Write-Error "未找到包文件: $projectPackPath"
    }
    Seperator
}

Write-Info "所有项目打包完成！"